public class PwdNumInputView  extends RelativeLayout {

    Context mContext;
    private MoneyNumInputView mniView;
    private TextView[] tvList;
    private ImageView[] imgList;
    private GridView gridView;
    private TextView ishongbao;
    private ImageView imgCancel; 
    private ArrayList<Map<String, String>> valueList;
	//密码输入长度
    private int currentIndex = -1;

    public PwdNumInputView(Context context) {
        this(context, null);
    }

    public PwdNumInputView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;

        View view = View.inflate(context, R.layout.layout_popup_bottom, null);

        mniView = (MoneyNumInputView) view.findViewById(R.id.virtualKeyboardView);
        imgCancel = (ImageView) view.findViewById(R.id.img_cancel);
        gridView = virtualKeyboardView.getGridView();
		
		//list中，相比金额输入，少了一个点按钮
        valueList = new ArrayList<>();
        for (int i = 1; i < 13; i++) {
            Map<String, String> map = new HashMap<String, String>();
            if (i < 10) {
                map.put("name", String.valueOf(i));
            } else if (i == 10) {
                map.put("name", "");
            } else if (i == 11) {
                map.put("name", String.valueOf(0));
            } else if (i == 12) {
                map.put("name", "");
            }
            valueList.add(map);
        }
        initView(view);
		//由于密码输入不需要点，所以这里用moneynumadapter时，需要重写点击事件
        setupView();
        addView(view);
    }

    private void initView(View view) {

        tvList = new TextView[6];
        imgList = new ImageView[6];
		
        tvList[0] = (TextView) view.findViewById(R.id.tv_pass1);
        tvList[1] = (TextView) view.findViewById(R.id.tv_pass2);
        tvList[2] = (TextView) view.findViewById(R.id.tv_pass3);
        tvList[3] = (TextView) view.findViewById(R.id.tv_pass4);
        tvList[4] = (TextView) view.findViewById(R.id.tv_pass5);
        tvList[5] = (TextView) view.findViewById(R.id.tv_pass6);

        imgList[0] = (ImageView) view.findViewById(R.id.img_pass1);
        imgList[1] = (ImageView) view.findViewById(R.id.img_pass2);
        imgList[2] = (ImageView) view.findViewById(R.id.img_pass3);
        imgList[3] = (ImageView) view.findViewById(R.id.img_pass4);
        imgList[4] = (ImageView) view.findViewById(R.id.img_pass5);
        imgList[5] = (ImageView) view.findViewById(R.id.img_pass6);

    }

    private void setupView() {
        MoneyNumAdapter mnAdapter = new MoneyNumAdapter(mContext, valueList);
        gridView.setAdapter(mnAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position < 11 && position != 9) {    //点击0~9按钮
                    if (currentIndex >= -1 && currentIndex < 5) {
                        ++currentIndex;
                        tvList[currentIndex].setText(valueList.get(position).get("name"));
                        tvList[currentIndex].setVisibility(View.INVISIBLE);
                        imgList[currentIndex].setVisibility(View.VISIBLE);
                    }
                } else {
                    if (position == 11) {
                        if (currentIndex - 1 >= -1) {
                            tvList[currentIndex].setText("");
                            tvList[currentIndex].setVisibility(View.VISIBLE);
                            imgList[currentIndex].setVisibility(View.INVISIBLE);
                            currentIndex--;
                        }
                    }
                }
            }
        });
    }

    //设置监听方法，在第6位输入完成后触发
    public void setOnFinishListener(final Listener_PwdFinish pass) {
        tvList[5].addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() == 1) {
                    String strPassword = "";     //每次触发都要先将strPassword置空，再重新获取，避免由于输入删除再输入造成混乱
                    for (int i = 0; i < 6; i++) {
                        strPassword += tvList[i].getText().toString().trim();
                    }
                    pass.done(strPassword);//接口中要实现的方法，完成密码输入完成后的响应逻辑
                }
            }
        });
    }

    public MoneyNumInputView getMoneyNumInputView() {
        return MoneyNumInputView;
    }

    public ImageView getImgCancel() {
        return imgCancel;
    }

}
